package com.chisheng.student.servlet;

import com.chisheng.common.Page;
import com.chisheng.service.impl.StudentServiceImpl;
import com.chisheng.common.StringUtils;
import com.chisheng.student.Student;
import com.chisheng.student.StudentUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Objects;

@WebServlet("/remove")
public class RemoveServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String id = request.getParameter("id");
        if (StringUtils.isNotEmpty(id)) {
            // 移除学生
            StudentServiceImpl studentService = new StudentServiceImpl();
            boolean result = studentService.remove(Integer.parseInt(id));
            if (result) {
                request.setAttribute("status", true);
                request.setAttribute("message", "移除成功");
            } else {
                request.setAttribute("status", false);
                request.setAttribute("message", "移除失败");
            }
        } else {
            request.setAttribute("status", false);
            request.setAttribute("message", "参数异常");
        }

        // 请求转发
        String url = String.format("/page?current=%d&size=%d", 1, 12);
        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
